package com.fenyin.samples.study.jdk.collections.map;  

import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
/**
 * 
 *计算Hash的方法
 *@Author:fengzhichao  
 *@Since:2012-12-22  
 *@Version:1.1.0
 */
public class HashMethod {
	
	private static final Log log = LogFactory.getLog(HashMethod.class);
	
	/**
	 * HashMap的hash方法
	 * @param h
	 * @return  
	 * @Description:
	 */
	public static int hashmap_hash(int h) {
        // This function ensures that hashCodes that differ only by
        // constant multiples at each bit position have a bounded
        // number of collisions (approximately 8 at default load factor).
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }

    /**
     * HashMap的hash查找方法
     * @param h
     * @param length
     * @return  
     * @Description:
     */
    public static int hashmap_indexFor(int h, int length) {
        return h & (length-1);
    }
    
    public static void main(String[] args){
    	for(int i= 1;i<=50;i++){
    		log.info("init:"+i+" hash:"+hashmap_hash(i)+" index:"+hashmap_indexFor(hashmap_hash(i),16));
    	}
    	
//    	HashMap<Integer,String> hashMap = new HashMap<Integer,String>();
//    	hashMap.put(1, "fengzhichao");
//    	hashMap.put(35, "caimin");
//    	hashMap.put(50, "huangzexiang");
    	
    	
//    	LinkedHashMap<Integer,String> linkHashMap = new LinkedHashMap<Integer,String>(16,0.99f,false);
//    	//头节点是Null
//    	linkHashMap.put(1,  "fengzhichao1");
//    	linkHashMap.put(35, "fengzhichao2");
//    	linkHashMap.put(50, "fengzhichao3");
//    	linkHashMap.put(16, "fengzhichaoT");
    	
    	ConcurrentHashMap<Integer,String> concurrentHashMap = new ConcurrentHashMap<Integer,String>();
    	concurrentHashMap.put(1,  "fengzhichao1");
    	concurrentHashMap.put(35, "fengzhichao2");
    	concurrentHashMap.put(50, "fengzhichao3");
    	concurrentHashMap.put(16, "fengzhichaoT");
    	
    	
    }
	
}
